package com.fenghongzhang;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fenghongzhang.day008.MainActivity;
import com.fenghongzhang.day008.R;
import com.fenghongzhang.day008.adapter.VpAdapter;
import com.fenghongzhang.day008.fragment.OneFragment;
import com.fenghongzhang.day008.fragment.ThreeFragment;
import com.fenghongzhang.day008.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager vp;
    private LinearLayout ll;


    private  List<ImageView> imageViewList;
    private ThreeFragment threeFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        ll = (LinearLayout) findViewById(R.id.ll);
        vp = (ViewPager) findViewById(R.id.vp);


        //1,
        SharedPreferences login = getSharedPreferences("open", MODE_PRIVATE);
        boolean isOpen = login.getBoolean("isOpen", false);
        if(isOpen){
            //跳
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }





        //创建数据源
        final List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
         threeFragment = new ThreeFragment();
        list.add(threeFragment);
        VpAdapter vpAdapter = new VpAdapter(getSupportFragmentManager(), list);

        //设置适配器
        vp.setAdapter(vpAdapter);


        //为了颜色的问题,把所有iv放到集合中. 遍历改颜色
       imageViewList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.circle_f);
            if(i == 0 ){
                imageView.setImageResource(R.drawable.circle_t);
            }
            imageViewList.add(imageView);
            ll.addView(imageView);
        }


        //自动翻页
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                //runOnUiThread在主线程中执行
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //主线程
                        vp.setCurrentItem(index);
                    }
                });
                index++;
                if(index > imageViewList.size()-1){
                    timer.cancel();
                }

            }
        },0,1500);



        //翻页监听器,当到自定页面的时候,呼叫倒计时
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                //最后一页
                if(position == list.size()-1){
                    threeFragment.handler.sendEmptyMessage(110);
                }
//
//                //改变小圆点的颜色
                for (int i = 0; i < imageViewList.size(); i++) {
                    //当前集合中的小圆点,就是当前页下面的小圆点
                    ImageView imageView = imageViewList.get(i);
                    if(i == position){
                        imageView.setImageResource(R.drawable.circle_t);
                    }else{
                        imageView.setImageResource(R.drawable.circle_f);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        /**
         * 第一打开软件的时候,给isOpen赋值.
         * 当第二次打开的会后,变为ture
         */
        SharedPreferences open = getSharedPreferences("open", MODE_PRIVATE);
        SharedPreferences.Editor edit = open.edit();
        edit.putBoolean("isOpen",true);
        edit.commit();

    }
}
